<template>
  <el-tabs v-model="activeTab">
    <el-tab-pane label="预约列表" name="list">
      <div class="mb-4">
        <el-button type="primary" @click="dialogVisible = true">新增预约</el-button>
        <el-button type="primary" @click="exportVisible = true">导出数据</el-button>
      </div>
      <el-table :data="reservations" style="width: 100%" border>
        <el-table-column
            v-for="col in config.tableColumns"
            :key="col.prop"
            :label="col.label"
            :prop="col.prop"
        >
          <template v-if="col.slot" #default="scope">
            <slot :name="col.slot" v-bind="scope" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <slot name="operation" :row="scope.row" />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="page"
          :page-size="pageSize"
          :total="total"
          @current-change="loadReservations"
          layout="total, prev, pager, next"
          background
          class="mt-4"
      />
    </el-tab-pane>

    <el-tab-pane label="待审核列表" name="review">
      <el-table :data="reviewReservations" style="width: 100%" border>
        <el-table-column
            v-for="col in config.tableColumns"
            :key="col.prop + '-review'"
            :label="col.label"
            :prop="col.prop"
        />
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <slot name="reviewOperation" :row="scope.row" />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="reviewPage"
          :page-size="pageSize"
          :total="reviewTotal"
          @current-change="loadReviewReservations"
          layout="total, prev, pager, next"
          background
          class="mt-4"
      />
    </el-tab-pane>
  </el-tabs>

  <el-dialog v-model="dialogVisible" :title="config.dialogTitle || '新增预约'" width="500px">
    <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
      <el-form-item
          v-for="field in config.formFields"
          :key="field.prop"
          :label="field.label"
          :prop="field.prop"
      >
        <component
            :is="getFieldComponent(field)"
            v-model="form[field.prop]"
            v-bind="field.props"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitForm">提交</el-button>
    </template>
  </el-dialog>

  <ExportDialog
      v-model="exportVisible"
      :type="config.exportType"
      @export="onExport"
  />
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
import { ElMessage } from 'element-plus'
import ExportDialog from '@/apps/admin/components/ExportDialog.vue'
import {
  getReservations,
  addReservation,
  getReservationsByNotReview
} from '@/utils/reservationApi'

const props = defineProps({
  type: String,
  config: Object
})

const activeTab = ref('list')
const dialogVisible = ref(false)
const exportVisible = ref(false)
const page = ref(1)
const reviewPage = ref(1)
const pageSize = 10
const total = ref(0)
const reviewTotal = ref(0)
const reservations = ref([])
const reviewReservations = ref([])

const formRef = ref(null)
const form = reactive({})
const rules = reactive({})

// 初始化字段
props.config.formFields.forEach(field => {
  form[field.prop] = field.default || ''
  if (field.required) {
    rules[field.prop] = [{ required: true, message: `请输入${field.label}`, trigger: 'blur' }]
  }
})

const getFieldComponent = (field) => {
  switch (field.type) {
    case 'number': return 'el-input-number'
    case 'date': return 'el-date-picker'
    default: return 'el-input'
  }
}

const loadReservations = async () => {
  const res = await getReservations(props.type, page.value, pageSize)
  reservations.value = res.data.records || res.data
  total.value = res.data.total || reservations.value.length
}

const loadReviewReservations = async () => {
  const res = await getReservationsByNotReview(props.type, reviewPage.value, pageSize)
  reviewReservations.value = res.data.records || res.data
  reviewTotal.value = res.data.total || reviewReservations.value.length
}

const submitForm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      await addReservation(form)
      ElMessage.success('新增预约成功')
      dialogVisible.value = false
      await loadReservations()
    }
  })
}

const onExport = (params) => {
  console.log('导出参数:', params)
  // 可插入具体导出逻辑
}

loadReservations()
loadReviewReservations()
</script>

<style scoped>
.mb-4 { margin-bottom: 16px; }
.mt-4 { margin-top: 16px; }
</style>
